<?php

namespace App\middlewares;

use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;

/**
 *
 */
class Auth
{
    private $_user;

    public function __construct($ci)
    {
        $this->_user = new \App\models\User($ci);
    }

    public function __invoke(ServerRequestInterface $request,ResponseInterface $response,callable $next)
    {
        $token = $request->getQueryParams()['token'];
        if($this->_user->validateToken($token)){
            $response = $next($request,$response);
        }else{
            return $response->withJson([
                'code' => 401,
                'msg' => '验证失败！'
                ],401);
        }
        return $response;
    }
}
